package org.example.com.controller;

import java.util.UUID;

import org.example.com.pojo.Result;
import org.example.com.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



@RestController
public class FileUploadController {
	
	//上传图片到阿里云
	@PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        //获取要上传的文件名 例如: musk-01.png
        String originalFilename = file.getOriginalFilename();
        
        System.err.println("FileUploadController originalFilename: "+originalFilename);
        
        //保证文件的名字是唯一的,从而防止文件覆盖 例如: c578d5ae-0b8f-41a3-92b0-57d419b3711a.png
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        
        //System.err.println("FileUploadController filename: "+filename);
        String url = AliOssUtil.uploadFile(filename,file.getInputStream());
        
        return Result.success(url);
    }

}